//
//  TL_Localized_Manager.m
//  WXReader
//
//  Created by LL on 2020/9/7.
//  Copyright © 2020 Andrew. All rights reserved.
//

#import "TL_LocalizedManager.h"

static NSString *kUserLanguage = @"kUserLanguage";

@interface TL_LocalizedManager ()

/// 语言包Bundle
@property (nonatomic, strong, class) NSBundle *languageBundle;

/// 本地语言名称
@property (nonatomic, strong, class) NSString *userLanguageName;

@end

@implementation TL_LocalizedManager

@dynamic languageBundle;

@dynamic userLanguageName;

#pragma mark - Public
+ (NSString *)stringWithKey:(NSString *)key {
    if (self.languageBundle) {
        return [self.languageBundle localizedStringForKey:key value:nil table:@"InfoPlist"] ?: key;
    } else {
        return NSLocalizedString(key, nil);
    }
}

+ (void)setLanguage:(LocalizedLanguage)language {
    [[NSUserDefaults standardUserDefaults] setValue:language == LocalizedLanguageDefault ? nil : NSStringFromLocalizedLanguage(language) forKey:kUserLanguage];
    _languageBundle = nil;
    [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Switch_Language object:nil];
}

+ (NSString *)stringWithKey:(NSString *)key language:(LocalizedLanguage)language {
    if (language == LocalizedLanguageDefault) {
        return WXLocalizedString(key);
    }
    
    NSString *t_language = NSStringFromLocalizedLanguage(language);
    NSString *path = [[NSBundle mainBundle] pathForResource:t_language ofType:@"lproj"];
    NSBundle *languageBundle = [NSBundle bundleWithPath:path];
    if (languageBundle) {
        return [languageBundle localizedStringForKey:key value:nil table:@"InfoPlist"] ?: key;
    } else {
        return NSLocalizedString(key, nil);
    }
}

+ (LocalizedLanguage)userLanguage {
    return LocalizedLanguageFromNSString([[NSUserDefaults standardUserDefaults] valueForKey:kUserLanguage]);
}

+ (LocalizedLanguage)localizedLanguage {
    NSString *t_launguage = NSStringFromLocalizedLanguage(LocalizedLanguageDefault);
    return LocalizedLanguageFromNSString(t_launguage);
}

+ (NSString *)serverLocalized {
    return [self serverLocalized:[self userLanguage]];
}

+ (NSString *)serverLocalized:(LocalizedLanguage)language {
    switch (language) {
        case LocalizedLanguageSimplifiedChinese:
            return @"zh";
        case LocalizedLanguageTraditionalChinese:
            return @"tw";
        case LocalizedLanguageEnglish:
            return @"en";
        case LocalizedLanguageTail:
            return @"th";
        case LocalizedLanguageSpanish:
            return @"es";
        case LocalizedLanguagePortugal:
            return @"pt";
        case LocalizedLanguageDefault:
        {
            NSString *t_str = NSStringFromLocalizedLanguage(language);
            LocalizedLanguage t_language = LocalizedLanguageFromNSString(t_str);
            return [self serverLocalized:t_language];
        }
    }
}


#pragma mark - Private
/// 根据语言名称字符串获取指定语言枚举类型
LocalizedLanguage LocalizedLanguageFromNSString(NSString *language) {
    if ([language hasPrefix:@"en"]) {// 英文
        return LocalizedLanguageEnglish;
    }

    if ([language hasPrefix:@"zh-Hans"]) {// 简体中文
        return LocalizedLanguageSimplifiedChinese;
    }
    
    if ([language hasPrefix:@"zh-Hant"]) {// 繁体中文
        return LocalizedLanguageTraditionalChinese;
    }
    
    if ([language hasPrefix:@"th"]) {// 泰语
        return LocalizedLanguageTail;
    }
    if ([language hasPrefix:@"es"]) {
        return LocalizedLanguageSpanish;
    }
    if ([language hasPrefix:@"pt-PT"]) {
        return LocalizedLanguagePortugal;
    }
    return LocalizedLanguageDefault;
}

/// 根据语言枚举类型获取指定语言名称(如果匹配不到则会返回默认语言)
NSString * NSStringFromLocalizedLanguage(LocalizedLanguage language) {
    if (![WX_Force_Language isEqualToString:@""]) {
        return WX_Force_Language;
    }
    
    switch (language) {
        case LocalizedLanguageEnglish:
            return @"en";
        case LocalizedLanguageSimplifiedChinese:
            return @"zh-Hans";
        case LocalizedLanguageTraditionalChinese:
            return @"zh-Hant";
        case LocalizedLanguageTail:
            return @"th";
        case LocalizedLanguageSpanish:
            return @"es";
        case LocalizedLanguagePortugal:
            return @"pt-PT";
        case LocalizedLanguageDefault:
        {
            NSArray *languages = [NSLocale preferredLanguages];
            LocalizedLanguage t_language = LocalizedLanguageFromNSString(languages.firstObject);
            if (t_language == LocalizedLanguageDefault) return WX_Default_Language;
            return NSStringFromLocalizedLanguage(t_language);
        }
    }
}


#pragma mark - getter/setter
static NSBundle *_languageBundle = nil;
+ (NSBundle *)languageBundle {
    if (!_languageBundle) {
        NSString *path = [[NSBundle mainBundle] pathForResource:self.userLanguageName ofType:@"lproj"];
        _languageBundle = [NSBundle bundleWithPath:path];
    }
    return _languageBundle;
}

/// 获取用户设置的本地语言名称(如果匹配不到则会返回默认语言)
+ (NSString *)userLanguageName {
    NSString *userLanguage = [[NSUserDefaults standardUserDefaults] valueForKey:kUserLanguage];
    if (userLanguage) return userLanguage;
    return NSStringFromLocalizedLanguage(LocalizedLanguageDefault);
}

@end
